// page/component/orders/orders.js
import requst from '../../../util/Http.js'
Page({
  data:{
    address:{},
    hasAddress: false,
    total:0,
    id:0,
    userobj:{},
    status:1,
    orders:[
        // {id:1,title:'新鲜芹菜 半斤',image:'/image/s5.png',num:4,price:0.01},
        // {id:2,title:'素米 500g',image:'/image/s6.png',num:1,price:0.03}
      ]
  },
  onLoad(ops){
    this.setData({
      id:ops.id
    })
    requst('/Index/getOrderDetli',{id:ops.id},'post').then(res=>{
      console.log(res)
      this.setData({
        status:res.data.status,
        userobj:res.data,
        address:JSON.parse(res.data.snap_address),
        orders:JSON.parse(res.data.snap_items),
        total:res.data.total_price
      })
    })

  },

  onReady() {
    this.getTotalPrice();
  },
  
  onShow:function(){
    const self = this;
    wx.getStorage({
      key:'address',
      success(res) {
        self.setData({
          address: res.data,
          hasAddress: true
        })
      }
    })
  },

  /**
   * 计算总价
   */
  getTotalPrice() {
    let orders = this.data.orders;
    let total = 0;
    for(let i = 0; i < orders.length; i++) {
      total += orders[i].num * orders[i].price;
    }
    this.setData({
      total: total
    })
  },

  toPay() {
    let {userobj}=this.data;
    requst('/Index/OrderPay',{order_no:userobj.order_no,user_id:userobj.user_id,status:userobj.status},'post').then(res=>{
      console.log(res)
    })
    // wx.requestPayment({
    //   timeStamp: 'String1',
    //   nonceStr: 'String2',
    //   package: 'String3',
    //   signType: 'MD5',
    //   paySign: 'String4',
    //   success: function(res){
    //     console.log(res)
    //   },
    //   fail: function(res) {
    //     wx.showModal({
    //       title:'支付提示',
    //       content:'<text>',
    //       showCancel: false
    //     })
    //   }
    // })
  }
})